newsmenu = {}

newsmenu.listCat;
newsmenu.listNewsCat;

newsmenu.list = function(){
    main.init();
    fly.ajax({
        service:'/newsMenu/list',
        success: function(result){
            if(result.status){
                newsmenu.listCat = result.data;
                $('#content').html(fly.template('http://naima.vn/newsmenu/list.html', {
                    data:result.data
                }));                                
                newsmenu.printMenuList(0);
            }
            else{
                popup.msg(result.message);
            }
        }
    });
}

newsmenu.create = function(){
    popup.open('popup-newsmenu-form', 'Thêm menu tin tức mới', fly.template('http://naima.vn/newsmenu/form.html', {
        data:null
    }), [

    {
        title:'Thêm mới', 
        fn:function(){            
            fly.submit({
                id:'newsmenu-form',
                service: '/newsMenu/create',
                success: function(result){                    
                    if(result.status){
                        popup.msg(result.message);
                        popup.close('popup-newsmenu-form');
                        $('table tr').first().after('');
                        fly.ajax({
                            service:'/newsMenu/list',
                            success: function(rs){
                                if(rs.status){
                                    newsmenu.listCat = rs.data;
                                    $('#content').html(fly.template('http://naima.vn/newsmenu/list.html', {
                                        data:rs.data
                                    }));                                
                                    newsmenu.printMenuList(0);
                                }
                                else{
                                    popup.msg(rs.message);
                                }
                            }
                        });
                    }
                    else
                        popup.msg(result.message);
                }
            });
        }
    },
    {
        title:'Hủy bỏ',
        fn:function(){
            popup.close('popup-newsmenu-form');
        }
    }
    ]);
    fly.ajax({
        service:'/newsMenu/list',
        success: function(result){
            newsmenu.listCat = result.data;
            newsmenu.printCat(0);
        }
    });
    newsmenu.checkType();
    $('#targetType').change(function(){
        newsmenu.checkType();
    });
}


newsmenu.edit = function(id){
    fly.ajax({
        service: '/newsMenu/get',
        loading: false,
        data: {
            id: id
        },
        success: function(data){
            if(!data.status)
                popup.msg(data.message);
            else{
                popup.open('popup-editmenu-form', 'Chỉnh sửa menu tin tức', fly.template('http://naima.vn/newsmenu/form.html', {
                    data:data.data
                }), [

                {
                    title:'Chỉnh sửa', 
                    fn:function(){            
                        fly.submit({
                            id:'newsmenu-form',
                            service: '/newsMenu/edit/'+id,
                            success: function(result){
                                if(result.status){
                                    popup.msg(result.message);
                                    popup.close('popup-editmenu-form');
                                    $('table tr').first().after('');
                                    fly.ajax({
                                        service:'/newsMenu/list',
                                        success: function(rs){
                                            if(rs.status){
                                                newsmenu.listCat = rs.data;
                                                $('#content').html(fly.template('http://naima.vn/newsmenu/list.html', {
                                                    data:rs.data
                                                }));                                
                                                newsmenu.printMenuList(0);
                                            }
                                            else{
                                                popup.msg(rs.message);
                                            }
                                        }
                                    });
                                }
                                else
                                    popup.msg(result.message);
                            }
                        });
                    }
                },
                {
                    title:'Hủy bỏ',
                    fn:function(){
                        popup.close('popup-editmenu-form');
                    }
                }
                ]);
                fly.ajax({
                    service:'/newsMenu/list',
                    loading: false,
                    success: function(res){
                        newsmenu.listCat = res.data;
                        newsmenu.printCat(0);
                        $('#parentId').val(data.data.parentId); 
                    }
                });           
                fly.ajax({
                    service: '/newsCategory/list',
                    loading: false,
                    success: function(rs){
                        newsmenu.listNewsCat = rs.data;
                    }
                });
                $('#targetType').val(data.data.targetType);                
                newsmenu.checkType(data.data.targetId, data.data.targetUrl);
                $('#targetType').change(function(){
                    newsmenu.checkType(data.data.targetId, data.data.targetUrl);
                });
                fly.ajax({
                    service: '/news/get',
                    data: {
                        id: data.data.targetId
                    },
                    async: false,
                    loading: false,
                    success: function(rs){
                        $('input[name=newsName]').val(rs.data.name);
                        $('.newsId').val(rs.data.id);
                        $('#targetId').val(rs.data.categoryId);
                    }
                });
            }
        }
    });    
}

newsmenu.del = function(id){
    popup.confirm("Bạn có chắc chắn muốn xóa menu này?", function(){
        fly.ajax({
            service: '/newsMenu/delete/',
            data: {
                id: id
            },
            success: function(result){
                popup.msg(result.message);
                $('.table tr[for='+id+']').remove();
                $.each(result.data, function(){
                    var tmp = this;
                    $('.table tr[for='+tmp.id+']').remove();
                });
            }
        });
    });
}
newsmenu.level = 0;
newsmenu.printCat = function(parentId){
    $.each(newsmenu.listCat, function(){
        var cat = this;
        if(cat.parentId == parentId){
            newsmenu.level ++;
            var tmp = "";
            var i = newsmenu.level;
            while(i){
                tmp += "- - ";
                i--;
            }
            $('#parentId').append('<option value="'+cat.id+'">'+tmp+cat.name+'</option>');
            newsmenu.printCat(cat.id);
            newsmenu.level --;
        }
    });
}

newsmenu.printMenuList = function(parentId){
    $.each(newsmenu.listCat, function(){
        var cat = this;
        if(cat.parentId == parentId){
            newsmenu.level ++;
            var tmp = "";
            var i = newsmenu.level;
            while(i){
                tmp += "- - ";
                i--;
            }
            
            if(cat.targetType == 1)
                cat.targetType = "Danh mục tin tức";
            else if(cat.targetType == 2)
                cat.targetType = "Tin tức";
            else
                cat.targetType = "Liên kết ngoài";
            
            if(cat.published == 1)
                cat.published = '<a onclick="newsmenu.changeStatus('+ cat.id +','+ newsmenu.level +')"><img src="'+ fly.assetsUrl +'/images/icon-enable.png" /></a>';
            else
                cat.published = '<a onclick="newsmenu.changeStatus('+ cat.id +','+ newsmenu.level +')"><img src="'+ fly.assetsUrl +'/images/icon-disable.png" /></a>';  
            
                
            $('table').append('<tr level="'+newsmenu.level+'" align="center" for="'+cat.id+'">\
                <td><input type="checkbox" for="'+cat.id+'"/></td>\
                <td><p>'+cat.id+'</p></td>\
                <td style="padding-left:50px;text-align:left"><p>'+tmp+cat.name+'</p></td>\
                <td><p>'+cat.targetType+'</p></td>\
                <td><p>'+cat.targetId+'</p></td>\
                <td><p>'+cat.targetUrl+'</p></td>\
                <td><p>'+cat.order+'</p></td>\
                <td><p>'+cat.published+'</p></td>\
                <td><p><a onclick="newsmenu.edit('+cat.id+')">Sửa</a></p></td>\
                <td><p><a onclick="newsmenu.del('+cat.id+')">Xóa</a></p></td>\
            </tr>');
            newsmenu.printMenuList(cat.id);
            newsmenu.level --;
        }
    })
}
newsmenu.checkAll = function(){
    if($('#list-checkall').is(":checked"))
        $('input[type="checkbox"]').attr('checked','checked');    
    else
        $('input[type="checkbox"]').removeAttr('checked');
}
newsmenu.delAll = function(){
    var data = Array();
    var i = 0;
    $('input[type=checkbox][for]').each(function(){
        if($(this).is(':checked')){
            data[i] = parseInt($(this).attr('for'));
            i++;
        }
    });
    if(data.length <= 0){
        popup.msg("Bạn chưa chọn menu nào.");
        return;
    }
    popup.confirm("Bạn có chắc chắn muốn xóa những menu này?", function(){
        fly.ajax({
            service: '/newsMenu/deleteall',
            data:{
                ids:JSON.stringify(data)
            },
            success: function(result){
                popup.msg(result.message);
                for(var i=0; i<result.data.length; i++){
                    $('.table tr[for='+result.data[i]+']').remove();
                }
            }
        });
    });
}
newsmenu.checkType = function(targetId, targetUrl){   
    if($('#targetType').val() == 1){
        $('.targetUrl').hide();
        $('.targetId').show();
        $('.targetId').show().html('<label>Id liên kết:</label><select id="targetId" name="NewsMenuForm[targetId]" style="width:310px;margin-left:5px" value="'+targetId+'"></select>');        
        
        newsmenu.printNewsCat(0);
        $('select[name="targetId"]').val(targetId);
    }
    else{
        if($('#targetType').val() == 2){  
            $('.targetUrl').hide();
            $('.targetId').show().html('<li><label>Danh mục:</label>\
                                            <select id="targetId" value="" style="width: 300px"><option value="0">Tất cả</option></select></li>\
                                            <li><label>Tin tức:</label>\
                                            <input name="newsName" value="" style="width: 300px"/></li>\
                                            <li><div class="auto-complete">\
                                            <div><li/>\
                                            <li/><label >Id tin tức:</label>\
                                            <input name="NewsMenuForm[targetId]" class="newsId" style="width:300px" value=""/>\
                                        <li/>');
            newsmenu.printNewsCat(0);            
            $('input[name=newsName]').keyup(function() {
                if($(this).val() != ''){
                    newsmenu.delay(function(){
                        fly.ajax({
                            service: '/news/list',
                            data: {
                                title: $('input[name=newsName]').val(), 
                                categoryId : $('#targetId').val(),
                                pageIndex: 0,
                                pageSize: 10
                            },
                            success: function(rs){
                                if(rs.status){
                                    $('.auto-complete').show().html('');
                                    if(rs.data.data.length>=1){
                                        $.each(rs.data.data, function(){
                                            $('.auto-complete').append('<label newsId="'+this.id+'" newsName="'+this.name+'">'+this.name+'</label>');
                                        });
                                        $('label[newsId]').click(function(){
                                            $('.auto-complete').hide();
                                            $('input[name=newsName]').val($(this).attr('newsName'));
                                            $('.newsId').val($(this).attr('newsId'));
                                        });
                                    }
                                }
                            }
                        });
                    }, 500 );
                }
            });
            $('body').click(function(){
                $('.auto-complete').hide();
            })
        }
        else{
            $('.targetId').hide();
            $('.targetUrl').show();
        }
    }
}
newsmenu.printNewsCat = function(parentId){
    $.each(newsmenu.listNewsCat, function(){
        var cat = this;
        if(cat.parentId == parentId){
            newsmenu.level ++;
            var tmp = "";
            var i = newsmenu.level;
            while(i){
                tmp += "- - ";
                i--;
            }
            $('#targetId').append('<option value="'+cat.id+'">'+tmp+cat.name+'</option>');
            newsmenu.printNewsCat(cat.id);
            newsmenu.level --;
        }
    });
}
$(document).ready(function(){    
    fly.ajax({
        service: '/newsCategory/list',
        loading: false,
        success: function(rs){
            newsmenu.listNewsCat = rs.data;
        }
    });
});

newsmenu.delay = (function(){
    var timer = 0;
    return function(callback, ms){
        clearTimeout (timer);
        timer = setTimeout(callback, ms);
    };
})();

newsmenu.changeStatus = function(id,level){
    fly.ajax({
        service: '/newsMenu/changeStatus',
        data: {
            id:id,       
        },
        success: function(result){    
            var tmp = "";
            var i = level;
            while(i){
                tmp += "- - ";
                i--;
            }  
            $('.table tr[for='+result.data.id+']').replaceWith(fly.template('http://naima.vn/newsmenu/row.html',{
                data:result.data, 
                lvl: level,
                tmp: tmp,
            }));
        }
    });

}